package top.hmtools.mapperUnit;

import org.springframework.stereotype.Component;

import top.hmtools.base.StringTools;
import top.hmtools.beans.ColumnsInfoBean;
import top.hmtools.common.ECurdType;
import top.hmtools.common.TableNameTools;
import top.hmtools.system.SystemInfoTools;

/**
 * 生成获取所有数据的sql语句片段
 * @author HyboJ
 * 创建日期：2018-2-1下午8:08:15
 */
@Component
public class GetAllMapperUnit extends AMapperUnitBase {
	
    /**
     * 获取该类是增删改查的哪一种
     * 方法说明
     * 输入参数说明
     * 输出参数说明
     */
    public ECurdType getCurdType() {
        return ECurdType.READ;
    }

    /**
     * select 
     * ...
     * from
     * table
     * <br>生成字符串内容
     * 方法说明
     * 输入参数说明
     * 输出参数说明
     */
    public String createContent(String basePackage,String methodName,ColumnsInfoBean... beans) {
        StringBuffer result = new StringBuffer();
        String tableAliseName = "aa";
        if(StringTools.isBlank(methodName)){
            methodName = "getAll";
        }
        result.append("<select id=\""+methodName+"\" resultType=\""+TableNameTools.getBeanName(basePackage, beans[0].getTable_name())+"Bean\">");
        result.append(SystemInfoTools.getOSLineSeparator());
        
        result.append("SELECT");
        result.append(SystemInfoTools.getOSLineSeparator());
        
        for(ColumnsInfoBean bean:beans){
        	result.append(tableAliseName+"."+bean.getColumn_name());
        	boolean useCamelName = this.gmConfiguration.isUseCamelName();
        	if(useCamelName){
        		result.append("  AS  "+TableNameTools.underlineToCamelByRegex(bean.getColumn_name())+",");
        	}else{
        		result.append(",");
        	}
        	result.append(SystemInfoTools.getOSLineSeparator());
        }
        int lastIndexOf = result.lastIndexOf(",");
        result = result.replace(lastIndexOf, lastIndexOf+1, "");
        
        result.append("FROM  "+beans[0].getTable_name()+" AS "+tableAliseName);
        result.append(SystemInfoTools.getOSLineSeparator());
        result.append("</select>");
        return result.toString();
    }

}
